package com.hypo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//@Component
//@Aspect
@Slf4j
public class MyAspect1 {

    /**
     * 切入点表达式抽取
     */
    @Pointcut("execution(* com.hypo.service.impl.*.*(..))")
    private void pt(){}
    /**
     * Before前置通知：目标对象的方法执行前，会执行Before通知
     * 作用：前置拦截，比如权限拦截
     */
    //@Before("execution(* com.hypo.service.impl.*.*(..))")
    @Before("pt()")
    public void before(JoinPoint jp) {
        String className = jp.getSignature().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}before ...", className, methodName);
    }

    //@Around("execution(* com.hypo.service.impl.*.*(..))")
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = pjp.proceed();
        long end = System.currentTimeMillis();
        String className = pjp.getSignature().getClass().getName();
        String methodName = pjp.getSignature().getName();
        log.info("{}.{}around ...{}", className, methodName, end - start);
        return proceed;
    }

    /**
     * After后置通知，相当于在方法中写了finaLLy
     */
    //@After("execution(* com.hypo.service.impl.*.*(..))")
    @After("pt()")
    public void after(JoinPoint jp) {
        String className = jp.getSignature().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}after ...", className, methodName);
    }

    /**
     * AfterReturning返回后通知
     */
    //@AfterReturning("execution(* com.hypo.service.impl.*.*(..))")
    @AfterReturning("pt()")
    public void afterReturning(JoinPoint jp) {
        String className = jp.getSignature().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}afterReturning ...", className, methodName);
    }

    /**
     * AfterThrowing异常通知
     */
    //@AfterThrowing("execution(* com.hypo.service.impl.*.*(..))")
    @AfterThrowing("pt()")
    public void afterThrowing(JoinPoint jp) {
        String className = jp.getSignature().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}afterThrowing ...", className, methodName);
    }
}
